package com.hycreate.platform.modules.crm.project;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.business.CrmSysDict;
import com.hycreate.platform.modules.crm.business.SysDictService;

@Controller
@RequestMapping(value = "${adminPath}/crm/project/projectcontact")
public class ProjectContactController extends BaseController {
	@Autowired
	private ProjectContactService projectContactService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private HycrmRemindService hycrmRemindService;

	@ModelAttribute
	public ProjectContact get(@RequestParam(required = false) String id) {// ,
	// @RequestParam(value="act.procInsId", required=false) String procInsId) {
		ProjectContact projectContact = null;
		if (StringUtils.isNotBlank(id)) {
			projectContact = projectContactService.get(id);
			// }else if (StringUtils.isNotBlank(procInsId)){
			// testAudit = testAuditService.getByProcInsId(procInsId);
		}
		if (projectContact == null) {
			projectContact = new ProjectContact();
		}
		return projectContact;
	}

	/**
	 * 查询项目联系人和项目状态
	 * 
	 * @param id
	 * @param projectcontact
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectcontact:addProjectContact")
	@RequestMapping(value = "findinfo")
	public String findinfo(String id,Model model) {
		Project project = projectService.get(id);
		//查找项目所在公司的所有联系人
		List<Contact> contact = projectContactService.findContact(project);
		//查询所有项目状态
		List<com.hycreate.platform.modules.sys.entity.Dict> dict = projectContactService.findDict();
		model.addAttribute("contact", contact);
		model.addAttribute("dict", dict);
		model.addAttribute("project", project);
		return "modules/crm/project/addprojectcontact";
	}

	/**
	 * 添加项目联系记录,更新项目状态
	 * 
	 * @param projectContact
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectcontact:addProjectContact")
	@RequestMapping(value = "addProjectContact")
	public String addProjectContact(ProjectContact projectContact, Model model,RedirectAttributes redirectAttributes) {
		
		CrmSysDict statusname = sysDictService.get(projectContact.getProStatus());
		if (statusname.getValue().equals("签订合同")) {
			hycrmRemindService.saveRemind(projectContact);// 插入一条回馈信息
			projectContact.setId(null);
		}
		projectContactService.save(projectContact); //添加联系记录
		projectService.changeStatus(projectContact); //修改项目状态
		addMessage(redirectAttributes,"添加联系记录成功");
		return "redirect:" + Global.getAdminPath() + "/crm/project/project/form?id="
				+ projectContact.getProjectId(); 
	}

	/**
	 * 查找项目公司所有联系人
	 * 
	 * @param projectcontact
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectcontact:addProjectContact")
	@RequestMapping(value = "findContact")
	public String findContact(String id, ProjectContact projectcontact,
			Model model) {
		List<Contact> contacts = projectContactService
				.findCustomer(projectcontact);
		Project project = projectService.get(id);
		model.addAttribute("project", project);
		model.addAttribute("projectId", id);
		model.addAttribute("contact", contacts);
		return "modules/crm/project/projectcontact";
	}

	/**
	 * 修改项目联系人
	 * 
	 * @param project
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "changeContact")
	public String changeContact(Project project, Model model,RedirectAttributes redirectAttributes) {
		projectService.changeContact(project);
		addMessage(redirectAttributes,"修改联系人成功");
		return "redirect:" + adminPath + "/project/project/form?id="
				+ project.getId();
	}
	/**
	 * 删除项目联系记录
	 * @param projectContact
	 * @return
	 */
	@RequiresPermissions("project:projectcontact:delete")
	@RequestMapping(value="delete")
	public String delete(ProjectContact projectContact){
		projectContact=projectContactService.get(projectContact.getId());
		projectContactService.delete(projectContact);
		return "redirect:"+Global.getAdminPath()+"/crm/project/project/form?id="+projectContact.getProjectId();
	}
}
